package org.gdstash.ui;

import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class GDSplashScreen extends JWindow {
    private Component owner;
    private JLabel lblImage;
    private JLabel lblText;

    public GDSplashScreen() {
        this((JFrame) null);
    }

    public GDSplashScreen(final JFrame frame) {
        super(frame);
        this.owner = frame;
        final JPanel panel = this.buildPanel();
        this.getContentPane().add(panel);
        this.pack();
        this.setLocationRelativeTo(this.owner);
        this.setVisible(true);
    }

    private JPanel buildPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel panel = new JPanel();
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        this.lblImage = new JLabel(GDImagePool.iconLoading);
        (this.lblText = new JLabel(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_LOADING"))).setFont(fntLabel);
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage).addComponent(this.lblText));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage)).addGroup(layout.createParallelGroup().addComponent(this.lblText));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblImage, this.lblText);
        panel.setBorder(compound);
        return panel;
    }

    public void setText(final String text) {
        this.lblText.setText(text);
    }
}
